﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace HemasHospitals.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class LoginScreen : Page
    {
        internal Frame rootFrame;

        public LoginScreen()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            rootFrame = new Frame();
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            LoginCommon();
        }

        private void Password_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                if (e.KeyStatus.RepeatCount == 1)
                {
                    LoginCommon();
                }
            }
        }

        private void DocName_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                if (e.KeyStatus.RepeatCount == 1)
                {
                    password_txt.Focus(FocusState.Programmatic);
                }
            }
        }

        private async void LoginCommon()
        {
            string doctorname = doctorname_txt.Text.Trim();
            string password = password_txt.Password;

            //Username: doc  |  Password: 123

            if (string.IsNullOrWhiteSpace(doctorname))
            {
                MessageDialog dlg = new MessageDialog("Doctor name can't be empty", "Hemas Hospitals");
                await dlg.ShowAsync();
            }
            else if (string.IsNullOrWhiteSpace(password))
            {
                MessageDialog dlg = new MessageDialog("Password can't be empty", "Hemas Hospitals");
                await dlg.ShowAsync();
            }
            else
            {
                if (doctorname.ToLower() == "doc" && password == "123")
                {
                    rootFrame.Navigate(typeof(DoctorDetailsScreen));

                    // Place the frame in the current Window 
                    Window.Current.Content = rootFrame;
                }
                else
                {
                    MessageDialog dlg = new MessageDialog("Login credentails are incorrect", "Hemas Hospitals");
                    await dlg.ShowAsync();
                }
            }
        }       
    }
}
